class MapController < ApplicationController

  def index
    @pings = Ping.find :all, :conditions => {:ping_type => 1}, :limit => 30, :order => "id DESC"
    respond_to do |format|
      format.html { render :template => "map/index", :layout => false }
      format.kml { render :template => "map/map.kml.erb", :layout => false }
      format.xml  { render :xml => @pings }
    end
  end
  
  def india
    @pings = Ping.find :all, :conditions => {:ping_type => 1}, :limit => 30, :order => "id DESC"
    @longitude = 85.9845
    @latitude = 26.24
    @zoom = 9
    respond_to do |format|
      format.html { render :template => "map/india", :layout => false }
      format.kml { render :template => "map/map.kml.erb", :layout => false }
      format.xml  { render :xml => @pings }
    end
  end
  
  def mapthegame
    @image = Dir.glob("public/garden/maps/*.jpg").last.gsub('public','')
    render :template => "map/mapthegame.html.erb", :layout => false
  end
  
  def tile
    # #1: 42.38146750675951,-71.15291118621826
    # #2: 42.35343807892731,-71.08394622802734
    # lon = 42.38146750675951
    # lat = -71.15291118621826
    # pings = Ping.find :all, :conditions => {:ping_type => 1}, :limit => 100, :order => "id DESC"
    # @tiles = []
    # 13.times do |row|
    #   23.times do |col|
    #     @tiles << [Ping.find :all, :conditions => {:ping_type => 1,:longitude => lon+(col*0.00121867078),:}, :limit => 100, :order => "id DESC"]
    #   end
    # end
  end
  
  def game
    @pings = Ping.find :all, :conditions => ['ping_type < 3'], :limit => 80, :order => "id DESC"
    respond_to do |format|
      format.html { render :template => "map/index", :layout => false }
      format.kml { render :template => "map/game.kml.erb", :layout => false }
      format.xml  { render :xml => @pings }
    end
  end
  
  def refresh
  end

  def refresh_back
    process_messages
    render :text => "Processed"+Time.now.to_s
  end
  
  def test
    # @pings = []
    # @tweets = Tweet.user_and_friends_timeline(:nextmap)
    # @tweets.each do |tweet|
    #   ping = tweet.save_as_ping
    #   @pings << ping unless ping == false
    # end
    # render :xml => @pings
    # since = (Time.now).strftime("%a%2C+%d+%b+%Y+%H%3A%M%3A%S+GMT")
    # render :text => Tweet.find(:all, :from=>"/statuses/friends_timeline/nextmap.xml?since="+since).size

    tweet = Tweet.new
    tweet.update_status("hey testing")
    render :text => "done"
  end

  def twitter
    @pings = Tweet.user_and_friends_timeline(:nextmap)
    respond_to do |format|
      format.html { render :template => "map/index", :layout => false }
      format.kml { render :template => "map/map.kml.erb", :layout => false }
      format.xml  { render :xml => @pings }
    end
  end
  
  def pings
    @pings = Ping.find :all, :conditions => {:ping_type => 1}, :limit => 30, :order => "id DESC"
    respond_to do |format|
      format.html { render :template => "map/index", :layout => false }
      format.kml { render :template => "map/map.kml.erb", :layout => false }
      format.xml  { render :xml => @pings }
    end
  end
  
  def phones
    @phones = Phone.find :all, :limit => 30, :order => "id DESC"
    pings = []
    @phones.each do |phone|
      pings << phone.last_ping
    end
    @pings = Ping.find pings
    respond_to do |format|
      format.html { render :template => "map/index", :layout => false }
      format.kml { render :template => "map/map.kml.erb", :layout => false }
      format.xml  { render :template => "map/current.xml.erb" }
    end
  end
  
  # GET /phones/current.xml
  def current
    @phones = Phone.find :all, :limit => 30, :order => "id DESC"
    pings = []
    @phones.each do |phone|
      pings << phone.last_ping
    end
    @pings = Ping.find pings
    respond_to do |format|
      format.xml  { render :collection => @pings, :template => "current" }
    end
  end
  
  def subscribe
    respond_to do |format|
      format.kml { render :template => "map/subscribe.kml.erb", :layout => false }
    end
  end

  def show_ping
    @ping = Ping.find(params[:ping_id]);
    @keyvalues = @ping.keyvalues;
  end
end
